package com.lottery.ruok.lottrey.ui.component;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.applozic.mobicomkit.contact.AppContactService;
import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.activity.ChatRecodeActivity;
import com.lottery.ruok.lottrey.chat.RongCloudChatActivityHelper;
import com.lottery.ruok.lottrey.chat.activity.ConversationListActivity;
import com.lottery.ruok.lottrey.chat.activity.MyConversationListActivity;
import com.lottery.ruok.lottrey.chat.event.manager.UnReadMessageManager;
import com.lottery.ruok.lottrey.retrofitModule.response.GetRoomUsersResponse;
import com.lottery.ruok.lottrey.userInfo.UserInfo;

import butterknife.BindView;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;
import glideHelp.GlideHelper;
import io.rong.imlib.RongIMClient;
import utils.Utils;

public class ChannelItemView extends BaseRelativeLayout {
    public static final int TYPE_SEARCH = 2;
    @BindView(R.id.iv_icon)
    CircleImageView ivIcon;
    @BindView(R.id.tv_bubble)
    TextView tvBubble;
    @BindView(R.id.iv_online)
    ImageView ivOnline;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_jiaose)
    TextView tvJiaose;
    @BindView(R.id.tv_privite_chat)
    TextView tvPriviteChat;
    @BindView(R.id.iv_pre_icon)
    ImageView ivPreIcon;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_more)
    TextView tvMore;
    @BindView(R.id.view_header)
    LinearLayout viewHeader;
    @BindView(R.id.iv_delete)
    ImageView ivDelete;
    private GetRoomUsersResponse.ListBean data;
    private AppContactService appContactService;
    private String title;
    private int type;
    private OnChannelEventListener onChannelEventListener;

    public ChannelItemView(Context context) {
        super(context);
    }

    public ChannelItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ChannelItemView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void init(Context context, AttributeSet attrs, int defStyleAttr) {

    }

    @Override
    protected void initView(Context context) {
        setContentView(R.layout.view_item_channel);
    }

    @Override
    protected void load(Context context) {

    }

    public void setData(GetRoomUsersResponse.ListBean data, String title) {
        this.data = data;
        this.title = title;
        updateView();
    }

    private void updateView() {
        if (Utils.isEmpty(title)) {
            viewHeader.setVisibility(GONE);
        } else {
            viewHeader.setVisibility(VISIBLE);
            if ("聊天记录".equals(title)) {
                tvMore.setVisibility(VISIBLE);
            } else {
                tvMore.setVisibility(GONE);
            }
            tvTitle.setText(title);
        }


        if (data != null) {
            tvName.setText(data.getNickname());
            tvJiaose.setText(getChatroom_role(data));
            if (!Utils.isEmpty(data.getAvatar())) {
                GlideHelper.loadImage(mContext, data.getAvatar(), ivIcon);
            } else {
                ivIcon.setImageResource(R.mipmap.chat_default);
            }

            UnReadMessageManager.getInstance().getPrivateUnreadCount(new RongIMClient.ResultCallback<Integer>() {
                @Override
                public void onSuccess(Integer integer) {
                    if (integer > 0) {
                        tvBubble.setVisibility(VISIBLE);
                        tvBubble.setText(integer + "");
                    } else {
                        tvBubble.setVisibility(GONE);
                        tvBubble.setText(0 + "");
                    }
                }

                @Override
                public void onError(RongIMClient.ErrorCode errorCode) {

                }
            }, data.getRy_id());


            int dataType = data.getDataType();
            switch (dataType) {
                case 0:
                    ivOnline.setVisibility(VISIBLE);
                    if (data.isOnline()) {
                        //在线
                        ivPreIcon.setVisibility(GONE);
                        ivOnline.setSelected(true);
                    } else {
                        ivPreIcon.setVisibility(VISIBLE);
                        ivOnline.setSelected(false);
                    }
                    tvPriviteChat.setVisibility(VISIBLE);
                    ivDelete.setVisibility(GONE);
                    break;
                case 1:
                    ivOnline.setVisibility(VISIBLE);
                    if (data.isOnline()) {
                        //在线
                        ivPreIcon.setVisibility(GONE);
                        ivOnline.setSelected(true);
                    } else {
                        ivPreIcon.setVisibility(VISIBLE);
                        ivOnline.setSelected(false);
                    }
                    ivPreIcon.setVisibility(GONE);
                    tvPriviteChat.setVisibility(VISIBLE);
                    ivDelete.setVisibility(GONE);
                    break;
                case 2:
                    ivOnline.setVisibility(VISIBLE);
                    if (data.isOnline()) {
                        //在线
                        ivPreIcon.setVisibility(GONE);
                        ivOnline.setSelected(true);
                    } else {
                        ivPreIcon.setVisibility(VISIBLE);
                        ivOnline.setSelected(false);
                    }
                    ivPreIcon.setVisibility(GONE);
                    tvPriviteChat.setVisibility(GONE);
                    ivDelete.setVisibility(VISIBLE);
                    break;
            }
        }
    }

    private String getChatroom_role(GetRoomUsersResponse.ListBean data) {
        String result = "";
        String role = data.getChatroom_role();
        if ("1".equals(role)) {
            //普通会员
            result = "会员";
        } else if ("2".equals(role)) {
            //超级管理元
            result = "超级管理员";
        } else if ("3".equals(role)) {
            //管理员
            result = "管理员";
        }
        return result;
    }

    public void setAppContactService(AppContactService appContactService) {
        this.appContactService = appContactService;
    }

    public AppContactService getAppContactService() {
        return appContactService;
    }

    @OnClick({R.id.tv_privite_chat, R.id.tv_more, R.id.iv_delete})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_privite_chat:
                if (UserInfo.getMyUserId().equals(data.getRy_id())) {
                    Utils.showToast("这是本人，不能和自己聊天");
                    return;
                }
                RongCloudChatActivityHelper.startPrivateChat(mContext, data.getRy_id(), data.getNickname());
                break;
            case R.id.tv_more:
              //  ChatRecodeActivity.startActivity((Activity) mContext);
                mContext.startActivity(new Intent(mContext, MyConversationListActivity.class));
                break;
            case R.id.iv_delete:
                deleteRecode();
                break;
        }
    }

    private void deleteRecode() {
        if (onChannelEventListener != null) {
            onChannelEventListener.delete(data);
        }


    }


    public void setType(int type) {
        this.type = type;
    }

    public int getType() {
        return type;
    }

    public void setOnChannelEventListener(OnChannelEventListener onChannelEventListener) {
        this.onChannelEventListener = onChannelEventListener;
    }

    public interface OnChannelEventListener {
        void delete(GetRoomUsersResponse.ListBean data);
    }
}
